/*
 * CategoryModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.data.request.QueryCategoryNewGoodsRequest;
import com.yidiandao.business.data.request.QueryCategoryPriceGoodsRequest;
import com.yidiandao.business.data.request.QueryCategorySalesGoodsRequest;
import com.yidiandao.business.data.response.QueryGoodsListResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** 分类Model */
public class CategoryModel implements ICategoryModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DoubleGoods> mDoubleGoodsList = new ArrayList<>();

    private List<Goods> mGoodsList = new ArrayList<>();

    private int mGoodsPager;

    @Override
    public void loadCategoryNewGoodsList(
            final boolean isRefreshing,
            String categoryId,
            String keyword,
            String tag,
            final OnLoadCategoryGoodsListener listener) {

        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            mGoodsPager = 1;
        }

        QueryCategoryNewGoodsRequest request = new QueryCategoryNewGoodsRequest();

        request.setUrl(Urls.CATEGORY_GOODS_LIST);
        request.setTag(tag);
        //判断是否是搜索品牌和产地
        if (!PreferenceUtils.STRING_DEFAULT.equals(categoryId)) {
            request.setKeyword(keyword);
            request.setCategoryId(categoryId);
        } else {
            request.setCat_keyword(keyword);
        }
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(mGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setAddTime("1");

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        // pager 增加
                        mGoodsPager++;
                        List<Goods> goodsLines = response.getData().getGoods();
                        if (isRefreshing) {
                            mDoubleGoodsList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mDoubleGoodsList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mGoodsList,
                                    mDoubleGoodsList,
                                    goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadCategoryPriceGoodsList(final boolean isRefreshing, String categoryId,
            String keyword, boolean isSortUp, String tag,
            final OnLoadCategoryGoodsListener listener) {

        if (isRefreshing) {
            mGoodsPager = 1;
        }

        QueryCategoryPriceGoodsRequest request = new QueryCategoryPriceGoodsRequest();

        request.setUrl(Urls.CATEGORY_GOODS_LIST);
        request.setTag(tag);
        //判断是否是搜索品牌和产地
        if (!PreferenceUtils.STRING_DEFAULT.equals(categoryId)) {
            request.setKeyword(keyword);
            request.setCategoryId(categoryId);
        } else {
            request.setCat_keyword(keyword);
        }
        request.setPrice(isSortUp
                ? QueryCategoryPriceGoodsRequest.SORT_UP
                : QueryCategoryPriceGoodsRequest.SORT_DOWN);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(mGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        mGoodsPager++;

                        List<Goods> goodsLines = response.getData().getGoods();
                        if (isRefreshing) {
                            mDoubleGoodsList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mDoubleGoodsList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(mGoodsList, mDoubleGoodsList,
                                    goodsLines.size() == 0, response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void loadCategorySalesGoodsList(final boolean isRefreshing, String categoryId,
            String keyword, String tag, final OnLoadCategoryGoodsListener listener) {

        if (isRefreshing) {
            mGoodsPager = 1;
        }

        QueryCategorySalesGoodsRequest request = new QueryCategorySalesGoodsRequest();
        request.setUrl(Urls.CATEGORY_GOODS_LIST);
        request.setTag(tag);
        request.setSalesNum(QueryCategorySalesGoodsRequest.SORT_DOWN);
        //判断是否是搜索品牌和产地
        if (!PreferenceUtils.STRING_DEFAULT.equals(categoryId)) {
            request.setKeyword(keyword);
            request.setCategoryId(categoryId);
        } else {
            request.setCat_keyword(keyword);
        }
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(mGoodsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryGoodsListResponse.class,
                new NetListener<QueryGoodsListResponse>() {
                    @Override
                    public void onSuccess(QueryGoodsListResponse response) {

                        mGoodsPager++;

                        List<Goods> goodsLines = response.getData().getGoods();
                        if (isRefreshing) {
                            mDoubleGoodsList.clear();
                            mGoodsList.clear();
                        }
                        mGoodsList.addAll(goodsLines);

                        for (int i = 0; i < goodsLines.size(); i += 2) {
                            Goods goods1 = null;
                            Goods goods2 = null;
                            try {
                                goods1 = goodsLines.get(i);
                                goods2 = goodsLines.get(i + 1);
                            } catch (IndexOutOfBoundsException e) {
                            }
                            mDoubleGoodsList.add(new DoubleGoods(goods1, goods2));
                        }

                        if (listener != null) {
                            listener.onSuccess(mGoodsList, mDoubleGoodsList, goodsLines.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }
}
